package com.example.apricots.Utils;

import android.content.Context;
import android.os.Handler;
import android.util.Log;
import android.widget.Toast;

import androidx.annotation.NonNull;
import androidx.biometric.BiometricManager;
import androidx.biometric.BiometricPrompt;
import androidx.fragment.app.FragmentActivity;

import java.util.concurrent.Executor;

/**
 * 源于：{https://blog.csdn.net/weixin_40303382/article/details/105383006?utm_medium=distribute.pc_relevant.none-task-blog-BlogCommendFromMachineLearnPai2-1.channel_param&depth_1-utm_source=distribute.pc_relevant.none-task-blog-BlogCommendFromMachineLearnPai2-1.channel_param}
 * 目前生物识别魅族手机无法识别到面部，只有指纹
 */
public class Biometric {
    private static final String TAG = Biometric.class.getSimpleName();
    private Handler handler = new Handler();
    public Biometric.AuthenticationcallBack callbackListener;
    private Executor executor = new Executor() {
        @Override
        public void execute(Runnable runnable) {
            handler.post(runnable);
        }
    };

    /**
     * 判断设备是否支持生物识别
     * @param context
     */
    public void detecBiometric(Context context) {
        BiometricManager biometricManager = BiometricManager.from(context);
        switch (biometricManager.canAuthenticate()) {
            case BiometricManager.BIOMETRIC_SUCCESS:
                Log.e(TAG, "应用可以进行生物识别技术进行身份验证。");
//                Toast.makeText(context, "应用可以进行生物识别技术进行身份验证", Toast.LENGTH_SHORT).show();
                break;
            case BiometricManager.BIOMETRIC_ERROR_NO_HARDWARE:
                Log.e(TAG, "该设备上没有搭载可用的生物特征功能。");
//                Toast.makeText(context, "该设备上没有搭载可用的生物特征功能", Toast.LENGTH_SHORT).show();
                break;
            case BiometricManager.BIOMETRIC_ERROR_HW_UNAVAILABLE:
                Log.e(TAG, "生物识别功能当前不可用。");
//                Toast.makeText(context, "生物识别功能当前不可用", Toast.LENGTH_SHORT).show();
                break;
            case BiometricManager.BIOMETRIC_ERROR_NONE_ENROLLED:
                Log.e(TAG, "用户没有录入生物识别数据。");
//                Toast.makeText(context, "用户没有录入生物识别数据", Toast.LENGTH_SHORT).show();
                break;
        }
    }

    //生物认证的setting
    public void showBiometricPrompt(Context context, Biometric.AuthenticationcallBack callbackListener) {
        BiometricPrompt.PromptInfo promptInfo =
                new BiometricPrompt.PromptInfo.Builder()
                        .setTitle("Biometric login for my app") //设置大标题
                        .setSubtitle("Log in using your biometric credential") // 设置标题下的提示
                        .setNegativeButtonText("Cancel") //设置取消按钮
                        .build();

        //需要提供的参数callback
        BiometricPrompt biometricPrompt = new BiometricPrompt((FragmentActivity) context, executor, new BiometricPrompt.AuthenticationCallback() {
            @Override
            public void onAuthenticationError(int errorCode, @NonNull CharSequence errString) {
                super.onAuthenticationError(errorCode, errString);
                Toast.makeText(context,
                        "Authentication error: " + errString, Toast.LENGTH_SHORT)
                        .show();
            }
            //识别成功的回调
            @Override
            public void onAuthenticationSucceeded(@NonNull BiometricPrompt.AuthenticationResult result) {
                super.onAuthenticationSucceeded(result);
                BiometricPrompt.CryptoObject authenticatedCryptoObject =
                        result.getCryptoObject();
                callbackListener.success();
            }

            @Override
            public void onAuthenticationFailed() {
                super.onAuthenticationFailed();

                callbackListener.fail();
            }
        });
        biometricPrompt.authenticate(promptInfo);
    }


    public interface AuthenticationcallBack {
        void success();

        void fail();

    }
}
